package com.ldoublem.loadingviewlib.view;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.utils.Color;
import ohos.app.Context;

import com.ldoublem.loadingviewlib.view.base.LVBase;


public class LVPlayBall extends LVBase implements Component.EstimateSizeListener, Component.DrawTask {
    private Paint mPaint, mPaintCircle, mPaintBall;
    private float mPaintStrokeWidth;
    private float mHigh = 0f;
    private float mWidth = 0f;
    private float quadToStart = 0f;
    private float mRadius = 0f;
    private float mRadiusBall = 0f;
    private float ballY = 0f;
    private int getMeasuredHeight;
    Path path = new Path();

    public LVPlayBall(Context context) {
        super(context);
    }

    public LVPlayBall(Context context, AttrSet attrs) {
        super(context, attrs);
    }

    public LVPlayBall(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }



    private void initPaint() {
        addDrawTask(this);
        setEstimateSizeListener(this);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE_STYLE);
        mPaint.setColor(Color.WHITE);


        mPaintCircle = new Paint();
        mPaintCircle.setAntiAlias(true);
        mPaintCircle.setStyle(Paint.Style.STROKE_STYLE);
        mPaintCircle.setColor(Color.WHITE);


        mPaintBall = new Paint();
        mPaintBall.setAntiAlias(true);
        mPaintBall.setStyle(Paint.Style.FILL_STYLE);
        mPaintBall.setColor(Color.WHITE);


    }


    public void setViewColor(int color)
    {
        mPaint.setColor(new Color(color));
        mPaintCircle.setColor(new Color(color));
        invalidate();
    }
    public void setBallColor(int color)
    {
        mPaintBall.setColor(new Color(color));
        invalidate();
    }


    @Override
    protected void InitPaint() {
        initPaint();
    }

    @Override
    protected void OnAnimationUpdate(AnimatorValue valueAnimator, float v) {
        float value = v;

        if (value > 0.75) {


            quadToStart = mHigh / 2 - (1f - v) * mHigh / 3f;
        } else {
            quadToStart = mHigh / 2 + (1f - v) * mHigh / 3f;

        }

        if (value > 0.35f) {
            ballY = mHigh / 2 - (mHigh / 2 * value);
        } else {
            ballY = mHigh / 2 + (mHigh / 6 * value);
        }


        invalidate();
    }

    @Override
    protected void OnAnimationRepeat(Animator animation) {

    }





    @Override
    protected int OnStopAnim() {
        quadToStart = mHigh / 2;
        ballY = mHigh / 2;
        return 0;
    }

    @Override
    protected int SetAnimRepeatMode() {
        return REVERSE;
    }
    @Override
    protected void AinmIsRunning() {

    } @Override
    protected int SetAnimRepeatCount() {
        return INFINITE;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        path = new Path();
        path.moveTo(0 + mRadius * 2 + mPaintStrokeWidth, getMeasuredHeight / 2);
        path.quadTo(mWidth / 2, quadToStart, mWidth - mRadius * 2 - mPaintStrokeWidth, mHigh / 2);
        mPaint.setStrokeWidth(2);
        canvas.drawPath(path, mPaint);


        mPaintCircle.setStrokeWidth(mPaintStrokeWidth);
        canvas.drawCircle(mRadius + mPaintStrokeWidth, mHigh / 2, mRadius, mPaintCircle);
        canvas.drawCircle(mWidth - mRadius - mPaintStrokeWidth, mHigh / 2, mRadius, mPaintCircle);


        if (ballY - mRadiusBall > mRadiusBall) {
            canvas.drawCircle(mWidth / 2, ballY - mRadiusBall, mRadiusBall, mPaintBall);
        } else {
            canvas.drawCircle(mWidth / 2, mRadiusBall, mRadiusBall, mPaintBall);

        }
    }

    @Override
    public boolean onEstimateSize(int i, int i1) {
        getMeasuredHeight = EstimateSpec.getSize(i1);
        mHigh = EstimateSpec.getSize(i1);
        mWidth = EstimateSpec.getSize(i);
        quadToStart = mHigh / 2;
        mRadius = dip2px(3);
        mPaintStrokeWidth = 2;
        ballY = mHigh / 2;
        mRadiusBall = dip2px(4);
        return false;
    }
}
